#include "TimeLen.h"
/*
TimeLen的构造函数
*/

//TimeLen的构造函数
TimeLen::TimeLen()
{
	
}

//TimeLen的构造函数
TimeLen::TimeLen(time_t t) 
{
	set(t);
}

//TimeLen的构造函数
TimeLen::TimeLen(int d,int h,int m,int s)
{
	set(d,h,m,s);
}

//TimeLen的构造函数
TimeLen::TimeLen(const char *s)
{
	set(s);
}

//TimeLen的构造函数
TimeLen::TimeLen(const string &s)
{
	set(s);
}
/*end for a block*/


/*
//set functions for TimeLen
*/

//set set TimeLen from seconds
void TimeLen::set(time_t t)
{
	sec = t%60; t /= 60;
	min = t%60; t /= 60;
	hour = t%24; t /= 24;
	day = t;
}

//set TimeLen from d,h,m,s
void TimeLen::set(int d,int h,int m,int s)
{
	day = d; hour = h; min = m; sec = s;
	set(time_t(*this));
}

//set TimeLen from char[]
void TimeLen::set(const char *s)
{
	sscanf(s,"%d:%d:%d:%d",
			&day,&hour,&min,&sec);
	set(time_t(*this));
}

//set TimeLen from string
void TimeLen::set(const string &s)
{
	set(s.c_str());
}
/*end for a block*/


/*
类型转换函数
*/

TimeLen::operator time_t()const
{
	return ((day*24+hour)*60+min)*60+sec;
}

TimeLen::operator string()const
{
	ostringstream ans;
	ans<<day<<":"<<hour<<":"<<min<<":"<<sec;
	return string(ans.str());
}
/*end for a block*/


/*时长运算，
注意，由于本类表示的时长皆是1s的整数倍，
乘法是求时长的k倍
故除法是换算成秒之后的整除*/

TimeLen TimeLen::operator +(const TimeLen& b)const
{
	return TimeLen(time_t(*this)+time_t(b));
}

TimeLen TimeLen::operator -(const TimeLen& b)const
{
	return TimeLen(time_t(*this)-time_t(b));
}

//TimeLen*k
TimeLen TimeLen::operator *(int k)const
{
	return TimeLen(time_t(*this)*k);
}

//k*TimeLen,attention, this is not member method
TimeLen operator *(int k,const TimeLen& a)
{
	return a*k;
}

TimeLen TimeLen::operator /(int k)const
{
	return TimeLen(time_t(*this)/k);
}
/*end for a block*/